package cn.com.cfae.iras.doc.analyze.components.integrity;

import cn.com.cfae.iras.doc.analyze.beans.web.Detail;
import cn.com.cfae.iras.doc.analyze.components.BaseComponent;
import cn.com.cfae.iras.doc.analyze.components.beans.ComponentResult;
import cn.com.cfae.iras.doc.analyze.handUtil.HandingData;
import cn.com.cfae.iras.doc.analyze.handUtil.SearchKeywordUtil;
import cn.com.cfae.iras.doc.analyze.parse.model.ChapterModel;
import cn.com.cfae.iras.doc.analyze.parse.model.DocumentModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractItem;
import cn.com.cfae.iras.doc.analyze.parse.model.PageModel;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 架构完整性
 *
 * “均视为已同意及接受上述约定”这句话必须出现在M-11章节
 *
 *在募集说明书合规性检查页面添加“是否涉及投资人保护条款”选项，由人工判断是否触发该项规则检查；
 *
 * 预评/发行阶段
 */
@Component("I1025")
@Scope("prototype")
public class I1025 extends BaseComponent {
    private static Logger log = LoggerFactory.getLogger(I1025.class);

    @Autowired
    private HandingData handingData;
    @Autowired
    private SearchKeywordUtil searchKeywordUtil;
    @Override
    public List<ComponentResult> handlerM() {
        log.info("获取I1025组件M");
        List<ComponentResult> componentResults = checkRulesI1025(getCatalogId(),getCatalogId(),StringUtils.isNotEmpty(getChapterId())?getChapterId():"M-11",getRuleTitle());
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerBM() {
        log.info("获取I1025组件BM");
        List<ComponentResult> componentResults = checkRulesI1025(getCatalogId(),getCatalogId(),StringUtils.isNotEmpty(getChapterId())?getChapterId():"BM-11",getRuleTitle());
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerDM() {
        return null;
    }

    @Override
    public List<ComponentResult> handlerBDM() {
        return null;
    }

    private List<ComponentResult> checkRulesI1025(String model,String catalogId,String chapterId,String ruleName){
        DocumentModel extractData = super.getExtractData();
        List<ComponentResult> componentResults = new ArrayList<>();
        ComponentResult componentResult = null;
        ExtractItem extractItem = null;
        int pageNum = 0;
        List<ComponentResult.TextPosition> contentAndPositionList =new ArrayList<>();
        ComponentResult.TextPosition contentAndPosition =null;
        boolean flag = false;
        Double x = 0.0;
        Double y = 0.0;
        int startPage = 0;

        String regex = "均视为已同意及接受上述约定";
        try {
            if (extractData!=null){
                ChapterModel chapterModel = extractData.getChapterModel(model);
                startPage = chapterModel!=null?chapterModel.getStartPage():0;
                if (chapterModel != null){
                    List<PageModel> pageModelList = chapterModel.getPageModelList();
                    if (CollectionUtils.isNotEmpty(pageModelList)){

                        for (PageModel pageModel :pageModelList){
                           // Map<String, Object> containsKeyWordOfRegex2Lines = handingData.isContainsKeyWord2Lines(pageModel.getJsonTextData(), regex);
                            Map<String, Object> containsKeyWordOfRegex2Lines = searchKeywordUtil.isContainsKeywords2LiensOfSynonyms(pageModel.getJsonTextData(), regex,pageModel);
                            flag = (boolean)containsKeyWordOfRegex2Lines.get("flag");
                            if (flag){
                                pageNum = pageModel.getPageNumber();
                                pageNum = pageNum!=0?pageNum:startPage;
                                contentAndPositionList = (List<ComponentResult.TextPosition>)containsKeyWordOfRegex2Lines.get("position");
                                break;
                            }
                        }

                    }
                }
            }
            if (flag){
                componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"关键词检查-均视为已同意及接受上述约定","正确",false,getRegulationId(),pageNum,contentAndPositionList,"01");
                componentResults.add(componentResult);
            }else {
                componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"关键词检查-均视为已同意及接受上述约定","该文档披露内容不完整",true,getRegulationId(),startPage,contentAndPositionList,"02");
                componentResults.add(componentResult);
            }
        }catch (Exception e){
            componentResults.clear();
            componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"关键词检查-均视为已同意及接受上述约定","未识别,建议人工核对",true,getRegulationId(),startPage,contentAndPositionList,"03");
            componentResults.add(componentResult);
            log.error(e.getMessage(),e);
        }
        return componentResults;
    }
}
